﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace IGL.Core.Highscores
{
    public static class HighscoresStore
    {
        private const string _HighscoresFile = "$highscores.bin";
        private static Dictionary<string, HighscoresData> _HighscoresRepository;
        
        static HighscoresStore()
        {
            _HighscoresRepository = new Dictionary<string, HighscoresData>();
            LoadIn();
        }

        public static void SaveOut()
        {
            using (Stream s = File.Open(_HighscoresFile, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(s, _HighscoresRepository);
                s.Close();
            }
        }
        public static void LoadIn()
        {
            if (!File.Exists(_HighscoresFile))
                return; 

            using (Stream s = File.Open(_HighscoresFile, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                _HighscoresRepository = (Dictionary<string, HighscoresData>)bf.Deserialize(s);
                s.Close();
            }
        }
    }
}
